package com.help10000.rms.ui.adapter

import android.content.Context
import android.view.View
import android.widget.ImageView
import com.amir.common.api.ApiContents
import com.amir.common.holder.CommonViewHolder
import com.amir.common.mvp.base.BaseSingleItemTypeRecyclerViewAdapter
import com.amir.common.utils.DisplayUtil
import com.amir.common.utils.ImageHelper
import com.help10000.rms.R
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader

class OrderReportPhotoItemAdapter(
    context: Context?,
    dataList: List<String>,
    maxCount: Int,
    resId: Int
) :
    BaseSingleItemTypeRecyclerViewAdapter<String>(context, dataList, resId) {
    var itemHeight = 0
    var maxCount: Int = 0;

    init {
        this.maxCount = maxCount
        itemHeight = (DisplayUtil.getDensityWdith(context) - DisplayUtil.dip2px(context, 80f)) / 3
    }

    override fun bindData(holder: CommonViewHolder, data: String?, position: Int) {
        var layoutParams = holder.itemView.layoutParams
        layoutParams.height = itemHeight
        holder.itemView.layoutParams = layoutParams
        var imgThumb = holder.getView<ImageView>(R.id.iv_pic)
        var mIvClose = holder.getView<ImageView>(R.id.iv_close)
        var photoUrl = ""
        if (position == dataList.size) {
            imgThumb.setImageResource(R.mipmap.sczp)
            mIvClose.visibility = View.GONE
        } else {
            mIvClose.visibility = View.VISIBLE

            if (data!!.startsWith("file")) {
                photoUrl = data
            } else {
                photoUrl = ApiContents.ImageUrl + dataList[position]

            }
            ImageHelper.displayImage(mContext, photoUrl, imgThumb)


            holder.setOnClickListener(R.id.iv_close) {
                dataList.removeAt(position)
                notifyItemRemoved(position)
                notifyItemRangeChanged(position, itemCount - position)
            }
        }
        holder.setOnClickListener(R.id.iv_pic) {
            if (dataList.size < maxCount && position == dataList.size) {
                mOnItemClickListener.onItem(position)
            } else {
                var data = mutableListOf<Any>()
                dataList.forEach { item ->
                    if (item.startsWith("file"))
                        data.add(item)
                    else
                        data.add(ApiContents.ImageUrl + item)
                }
                XPopup.Builder(mContext)
                    .asImageViewer(
                        imgThumb,
                        position,
                        data,
                        null,
                        SmartGlideImageLoader()
                    )
                    .show()

//                XPopup.Builder(mContext)
//                    .asCustom(ViewBigImageDialog(mContext, photoUrl))
//                    .show()
            }
        }
    }

    override fun fetchData(position: Int): String? {
        if (position >= dataList.size)
            return null
        return super.fetchData(position)
    }


    override fun getItemCount(): Int {
        if (dataList.size == maxCount) {
            return maxCount
        }
        return dataList.size + 1
    }


}
